//
//  MyOrderTableViewController.m
//  1243
//
//  Created by 袖手旁觀 on 16/4/15.
//  Copyright © 2016年 1243. All rights reserved.
//

#import "MyOrderTableViewController.h"
#import "NetWorkTools.h"
#import "ReadLoginData.h"
#import "MyOrderTableViewCell.h"
#import "SVProgressHUD.h"

@interface MyOrderTableViewController ()<UIAlertViewDelegate>

@property (nonatomic,strong) NSMutableArray *OrderArray;

@end

@implementation MyOrderTableViewController

- (NSMutableArray *)OrderArray {
    if (!_OrderArray) {
        _OrderArray = [NSMutableArray array];
    }
    return _OrderArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.title = @"我的订单";
    
    self.tableView.rowHeight = 100;
    
    self.tableView.tableFooterView = [UIView new];
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    [SVProgressHUD show];
    [self orderDataSource];
    
}

- (void)orderDataSource {
    
    NSDictionary *dict = [ReadLoginData readLoginData];
    
    NSDictionary *params = @{
                             @"UserId":[dict objectForKey:@"Number"]
                             };
    
    [[NetWorkTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"QUEYORDERLIST" completeBlock:^(id object) {
        
        NSString *string = [object objectForKey:@"BackData"];
        NSArray *array = [NetWorkTools arrayWithJsonString:string];
        [self.OrderArray addObjectsFromArray:array];
        NSLog(@"%@",self.OrderArray);
        [self.tableView reloadData];
        [SVProgressHUD dismiss];
        if (self.OrderArray.count == 0) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"暂无订单" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            alert.tag = 10001;
            [alert show];
        }
    }];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (alertView.tag == 10001) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.OrderArray.count;
//    return self.OrderArray.count;
    NSLog(@"%ld",self.OrderArray.count)
}


/*
 TotalPrice 价格
 OrderNO 编号
 Size  大小
 Color 颜色
 ProductName 名称
 Count
 
 */

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier= @"myOrderCell";
    
    MyOrderTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (cell==nil) {
        cell= [[[NSBundle mainBundle] loadNibNamed:@"MyOrderTableViewCell" owner:nil options:nil] lastObject];
    }
    
    NSDictionary *dict = self.OrderArray[indexPath.row];

    cell.productLabel.text = [NSString stringWithFormat:@"商品名称:%@",dict[@"ProductName"]];
    cell.countLabel.text = [NSString stringWithFormat:@"商品数量:%@",dict[@"Count"]];
    
    if (dict[@"Color"] != [NSNull null]) {
        cell.colorLabel.text = [NSString stringWithFormat:@"颜色:%@",dict[@"Color"]];
    }else{
        cell.colorLabel.text = [NSString stringWithFormat:@"颜色:无"];
    }

    if ([dict objectForKey:@"Size"] != [NSNull null]) {
        cell.sizeLabel.text = [NSString stringWithFormat:@"大小:%@",dict[@"Size"]];
    }else{
        cell.sizeLabel.text = [NSString stringWithFormat:@"大小:正常"];
    }
    
    cell.priceLabel.text = [NSString stringWithFormat:@"价格:%@",dict[@"TotalPrice"]];
    cell.numberLabel.text = [NSString stringWithFormat:@"编号:%@",dict[@"OrderNO"]];
    NSString *createTime = [dict[@"CreateTime"] stringByReplacingOccurrencesOfString:@"T" withString:@" "];
    cell.createTimeLabel.text = [NSString stringWithFormat:@"订单时间:%@",createTime];
    
    
    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
